package com.dandelion.common.utils;

import org.apache.commons.lang3.StringUtils;

public class MaskUtils {
    public static String maskOrderNo(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return StringUtils.EMPTY;
        }

        StringBuilder builder = new StringBuilder();
        builder.append(orderNo.substring(0, 4));
        builder.append("****");
        builder.append(orderNo.substring(orderNo.length() - 2));

        return builder.toString();
    }

    public static void main(String[] args) {
        System.out.println(maskOrderNo("392701940240"));
    }

    public static String maskIdCardNo(String idCardNo) {
        if (StringUtils.isBlank(idCardNo)) {
            return StringUtils.EMPTY;
        }

        if (idCardNo.length() == 15) {
            StringBuilder buf = new StringBuilder(15);
            buf.append(idCardNo.substring(0, 7));
            buf.append("****");
            buf.append(idCardNo.substring(11, 15));
            return buf.toString();
        } else if (idCardNo.length() == 18) {
            StringBuilder buf = new StringBuilder(18);
            buf.append(idCardNo.substring(0, 10));
            buf.append("****");
            buf.append(idCardNo.substring(14, 18));
            return buf.toString();
        } else if (idCardNo.length() > 4) {
            StringBuilder buf = new StringBuilder(18);
            buf.append("****");
            buf.append(idCardNo.substring(idCardNo.length() - 4));
            return buf.toString();
        }

        return idCardNo;
    }

    public static String maskDistrict(String district) {
        return "****";
    }

    public static String maskUserName(String userName) {
        if (StringUtils.isBlank(userName)) {
            return StringUtils.EMPTY;
        }
        if (userName.length() == 1) {
            return userName;
        }
        StringBuilder buf = new StringBuilder(userName);

        if (userName.length() == 2 || userName.length() == 3) {
            buf.setCharAt(1, '*');
            return buf.toString();
        }
        buf.setCharAt(userName.length() - 2, '*');
        return buf.toString();
    }

    public static String maskMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return StringUtils.EMPTY;
        }
        if (mobile.length() == 11) {
            StringBuilder buf = new StringBuilder(11);
            buf.append(mobile.substring(0, 4));
            buf.append("***");
            buf.append(mobile.substring(7, 11));
            return buf.toString();
        }
        return mobile;
    }
}
